package org.aaron.demos;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

public class Handler2Activity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.handler2layout);
		System.out.println("MainThread-id" + Thread.currentThread().getId());
		HandlerThread handlerThread = new HandlerThread("HandlerThred");
		//如果未调用start()方法，getLooper()将抛出空指针异常
		handlerThread.start();
		MyHandler handler=new MyHandler(handlerThread.getLooper());
		Message message=handler.obtainMessage();
		message.arg1=1;
		message.sendToTarget();
	}

	class MyHandler extends Handler {
		public MyHandler() {
		}

		public MyHandler(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			System.out.println("HandlerThread-id" + Thread.currentThread().getId());
			super.handleMessage(msg);
			System.out.println(msg.arg1);
		}
	}

}
